<template>
  <div class="camera-card">
    <div class="camera-header">
      <div class="camera-name">{{ camera.name }}</div>
      <div class="camera-status">
        <span
          :class="[
            'status-indicator',
            { 'status-online': camera.status === 'online', 'status-offline': camera.status === 'offline' }
          ]"
        ></span>
        <span class="status-text">{{ camera.status === 'online' ? '在线' : '离线' }}</span>
      </div>
    </div>
    
    <div class="camera-preview" @click="showStream = true">
      <img v-if="camera.snapshot" :src="camera.snapshot" alt="Camera Snapshot" class="snapshot">
      <div v-else class="no-snapshot">
        <el-icon><VideoPlay /></el-icon>
        <span>点击查看视频流</span>
      </div>
    </div>
    
    <div class="camera-info">
      <div class="info-item">
        <el-icon><Monitor /></el-icon>
        <span>{{ camera.type || 'IP摄像头' }}</span>
      </div>
      <div class="info-item">
        <el-icon><Location /></el-icon>
        <span>{{ camera.location || '未知位置' }}</span>
      </div>
      <div class="info-item">
        <el-icon><Clock /></el-icon>
        <span>{{ formatLastActive(camera.lastActive) }}</span>
      </div>
    </div>
    
    <div class="camera-actions">
      <el-button
        v-if="camera.status === 'offline'"
        type="primary"
        size="small"
        @click="handleStart"
        :loading="actionLoading.start"
      >
        <el-icon><Play /></el-icon>
        启动
      </el-button>
      <el-button
        v-else
        type="danger"
        size="small"
        @click="handleStop"
        :loading="actionLoading.stop"
      >
        <el-icon><Stop /></el-icon>
        停止
      </el-button>
      <el-button
        type="info"
        size="small"
        @click="handleEdit"
        :loading="actionLoading.edit"
      >
        <el-icon><Edit /></el-icon>
        编辑
      </el-button>
      <el-button
        type="danger"
        size="small"
        @click="handleDelete"
        :loading="actionLoading.delete"
        plain
      >
        <el-icon><Delete /></el-icon>
        删除
      </el-button>
    </div>
    
    <!-- 视频流对话框 -->
    <el-dialog
      v-model="showStream"
      :title="camera.name + ' - 视频流'"
      width="80%"
      destroy-on-close
    >
      <div class="video-container">
        <video
          v-if="camera.status === 'online'"
          :src="getStreamUrl(camera.id)"
          controls
          autoplay
          muted
          class="video-stream"
        ></video>
        <div v-else class="no-stream">
          <el-icon><VideoPause /></el-icon>
          <span>摄像头当前离线，无法查看视频流</span>
        </div>
      </div>
      <template #footer>
        <el-button @click="showStream = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { VideoPlay, VideoPause, Monitor, Location, Clock, Play, Stop, Edit, Delete } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

// Props
const props = defineProps({
  camera: {
    type: Object,
    required: true
  }
})

// Emits
const emit = defineEmits(['start', 'stop', 'edit', 'delete'])

// 响应式数据
const showStream = ref(false)
const actionLoading = ref({
  start: false,
  stop: false,
  edit: false,
  delete: false
})

// 格式化最后活跃时间
const formatLastActive = (timestamp) => {
  if (!timestamp) return '从未活跃'
  
  const now = new Date()
  const lastActive = new Date(timestamp)
  const diffMs = now - lastActive
  const diffMins = Math.floor(diffMs / 60000)
  const diffHours = Math.floor(diffMs / 3600000)
  const diffDays = Math.floor(diffMs / 86400000)
  
  if (diffMins < 1) return '刚刚'
  if (diffMins < 60) return `${diffMins}分钟前`
  if (diffHours < 24) return `${diffHours}小时前`
  if (diffDays < 7) return `${diffDays}天前`
  
  return lastActive.toLocaleDateString('zh-CN')
}

// 获取视频流URL
const getStreamUrl = (cameraId) => {
  // 直接返回视频流推送端点，这是后端实际提供MJPEG流的端点
  return `http://localhost:5001/api/cameras/${cameraId}/video_feed`
}

// 处理启动摄像头
const handleStart = async () => {
  actionLoading.value.start = true
  try {
    await emit('start', props.camera.id)
    ElMessage.success('摄像头启动成功')
  } catch (error) {
    ElMessage.error('摄像头启动失败: ' + (error.message || '未知错误'))
  } finally {
    actionLoading.value.start = false
  }
}

// 处理停止摄像头
const handleStop = async () => {
  actionLoading.value.stop = true
  try {
    await emit('stop', props.camera.id)
    ElMessage.success('摄像头停止成功')
  } catch (error) {
    ElMessage.error('摄像头停止失败: ' + (error.message || '未知错误'))
  } finally {
    actionLoading.value.stop = false
  }
}

// 处理编辑摄像头
const handleEdit = () => {
  actionLoading.value.edit = true
  emit('edit', props.camera)
  actionLoading.value.edit = false
}

// 处理删除摄像头
const handleDelete = () => {
  emit('delete', props.camera.id)
}
</script>

<style scoped>
.camera-card {
  background: #ffffff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  padding: 16px;
  transition: transform 0.2s, box-shadow 0.2s;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.camera-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
}

.camera-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.camera-name {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 60%;
}

.camera-status {
  display: flex;
  align-items: center;
  gap: 6px;
}

.status-indicator {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
}

.status-online {
  background-color: #67c23a;
}

.status-offline {
  background-color: #909399;
}

.status-text {
  font-size: 12px;
  color: #606266;
}

.camera-preview {
  position: relative;
  height: 180px;
  border-radius: 4px;
  overflow: hidden;
  margin-bottom: 12px;
  cursor: pointer;
  background-color: #f5f7fa;
  display: flex;
  align-items: center;
  justify-content: center;
}

.snapshot {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s;
}

.camera-preview:hover .snapshot {
  transform: scale(1.05);
}

.no-snapshot {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #909399;
  gap: 8px;
}

.no-snapshot .el-icon {
  font-size: 24px;
}

.no-snapshot span {
  font-size: 14px;
}

.camera-info {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 12px;
  flex: 1;
}

.info-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: #606266;
}

.info-item .el-icon {
  font-size: 14px;
}

.camera-actions {
  display: flex;
  gap: 8px;
  justify-content: space-between;
}

.video-container {
  position: relative;
  padding-bottom: 56.25%; /* 16:9 Aspect Ratio */
  height: 0;
  overflow: hidden;
}

.video-stream {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.no-stream {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: #f5f7fa;
  color: #909399;
  gap: 12px;
}

.no-stream .el-icon {
  font-size: 48px;
}

.no-stream span {
  font-size: 16px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .camera-card {
    padding: 12px;
  }
  
  .camera-preview {
    height: 150px;
  }
  
  .camera-actions {
    flex-wrap: wrap;
  }
  
  .camera-actions .el-button {
    flex: 1;
    min-width: 80px;
  }
}
</style>